package net.huanger.gametools.mapeditor.ui;


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;


public class MapEditorMenuBar extends JMenuBar
{

  private static final long serialVersionUID = 1L;

  private MapEditorPanel mapEditorPanel;

  private JMenuItem importTitePic;

  private JMenuItem createMap;

  private JMenuItem exportMap;

  private JMenuItem importMap;

  public static FileFilter MDATA_FILTER = new FileFilter()
    {

      @Override
      public String getDescription()
      {
        return "*.mdata";
      }

      @Override
      public boolean accept(File f)
      {
        if (f.isDirectory())
          return true;
        if (f.getName().toLowerCase().endsWith(".mdata"))
          return true;
        return false;
      }
    };

  public MapEditorMenuBar(MapEditorPanel comp)
  {
    this.mapEditorPanel = comp;
    init();
  }

  private void init()
  {
    initModel();
    initComponent();
    initEventListener();
  }

  private void initModel()
  {

  }

  private void initComponent()
  {
    JMenu fileMenu = new JMenu("File");
    this.add(fileMenu);

    JMenu titeMenu = new JMenu("Tite");
    this.add(titeMenu);

    importTitePic = titeMenu.add("Import titePic");

    JMenu mapMenu = new JMenu("Map");
    this.add(mapMenu);

    createMap = mapMenu.add("Create Map");
    importMap = mapMenu.add("Import Map");
    exportMap = mapMenu.add("Export Map");
  }

  private void initEventListener()
  {
    importMap.addActionListener(new ActionListener()
      {

        @Override
        public void actionPerformed(ActionEvent e)
        {
          JFileChooser fileChooser = new JFileChooser(".");
          fileChooser.setFileFilter(MDATA_FILTER);
          int result = fileChooser.showOpenDialog(mapEditorPanel);
          if (result == JFileChooser.APPROVE_OPTION)
          {
            File f = fileChooser.getSelectedFile();
            try
            {
              mapEditorPanel.importMap(f);
            }
            catch (Exception e1)
            {
              e1.printStackTrace();
              JOptionPane.showMessageDialog(mapEditorPanel, e1.getMessage());
            }
          }
        }
      });
    exportMap.addActionListener(new ActionListener()
      {

        @Override
        public void actionPerformed(ActionEvent e)
        {
          JFileChooser fileChooser = new JFileChooser(".");
          fileChooser.setFileFilter(MDATA_FILTER);
          int result = fileChooser.showSaveDialog(mapEditorPanel);
          if (result == JFileChooser.APPROVE_OPTION)
          {
            File f = fileChooser.getSelectedFile();
            try
            {
              mapEditorPanel.exportMap(f);
            }
            catch (IOException e1)
            {
              JOptionPane.showMessageDialog(mapEditorPanel, e1.getMessage());
            }
          }
        }
      });
    createMap.addActionListener(new ActionListener()
      {

        @Override
        public void actionPerformed(ActionEvent e)
        {
          final JDialog dialog = new JDialog();
          dialog.setModal(true);
          dialog.setLayout(new FlowLayout());
          dialog.add(new JLabel("row:"));
          final JTextField comp2 = new JTextField(8);
          final JTextField comp = new JTextField(8);
          dialog.add(comp);
          dialog.add(new JLabel("column:"));
          dialog.add(comp2);
          JButton okbtn = new JButton("OK");
          okbtn.addActionListener(new ActionListener()
            {
              @Override
              public void actionPerformed(ActionEvent e)
              {
                try
                {
                  int row = Integer.parseInt(comp.getText().trim());
                  int column = Integer.parseInt(comp2.getText().trim());
                  mapEditorPanel.createMap(row, column,null);
                  dialog.dispose();
                }
                catch (NumberFormatException e1)
                {
                  JOptionPane.showMessageDialog(mapEditorPanel, "请输入数字！");
                }
              }
            });
          dialog.add(okbtn);
          dialog.setSize(100, 130);
          dialog.setLocationRelativeTo(mapEditorPanel);
          dialog.setVisible(true);

        }
      });
    importTitePic.addActionListener(new ActionListener()
      {

        @Override
        public void actionPerformed(ActionEvent e)
        {
          JFileChooser fileChooser = new JFileChooser(".");
          fileChooser.setFileFilter(new FileFilter()
            {

              @Override
              public String getDescription()
              {
                return Arrays.toString(ImageIO.getReaderFileSuffixes());
              }

              @Override
              public boolean accept(File f)
              {
                if (f.isDirectory())
                  return true;
                for (String str : ImageIO.getReaderFileSuffixes())
                {
                  if (f.getName().endsWith(str))
                  {
                    return true;
                  }
                }
                return false;
              }
            });
          int result = fileChooser.showOpenDialog(mapEditorPanel);
          if (result == JFileChooser.APPROVE_OPTION)
          {
            File file = fileChooser.getSelectedFile();
            try
            {
              mapEditorPanel.getTitePicCanvas().importTitePic(file);
            }
            catch (IOException e1)
            {
              JOptionPane.showMessageDialog(mapEditorPanel, e1.getMessage());
            }
          }
        }
      });
  }
}
